package com.shushan.mixedexam;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.BaseExerciseHeaderActivity;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.base.ScreenManager;
import com.shushan.mixedexam.oralcalculation.bean.OCsubjectBean;
import com.shushan.mixedexam.oralcalculation.bean.OralCalculateExerciseListBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.pages.StudyProgressSetActivity;
import com.shushan.pages.bean.TopicBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.DialogUtil;
import com.shushan.util.ImageUtils;
import com.shushan.util.ToolUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @ClassName: OralCalculateExerciseActivity
 * @Desciption: //口算练习考试activity
 * @author: zhangshihao
 * @date: 2018-06-28
 */
public class OralCalculateExerciseActivity extends BaseExerciseHeaderActivity implements View.OnClickListener{

    private static final String TAG = OralCalculateExerciseActivity.class.getSimpleName();

    private ImageView ivMathBook;
    private TextView tvProgressNow;
    private LinearLayout llSetProgress;
    private LinearLayout llPreLesson,llNextLesson;
    private Button btnStartExercise;

    private int progress_lock;
    private OCsubjectBean oCsubjectBean;

    private BabyApplication shuShanApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_oral_calculate_exercise);
        setTitle("口算练习");

        initView();
    }

    private void initView(){
        ivMathBook = findViewById(R.id.iv_math_book);
        tvProgressNow = findViewById(R.id.tv_progress_now_math);
        llSetProgress = findViewById(R.id.ll_set_progress_math);
        llSetProgress.setOnClickListener(this);
        llPreLesson = findViewById(R.id.ll_pre_lesson_math);
        llPreLesson.setOnClickListener(this);
        llNextLesson = findViewById(R.id.ll_next_lesson_math);
        llNextLesson.setOnClickListener(this);
        btnStartExercise = findViewById(R.id.btn_start_exercise);
        btnStartExercise.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData(){
        int childId = shuShanApplication.getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.OC_EXERCISE_LIST);
        UrlParse paramParse = new UrlCommParse(this).putValue("child_id",childId);
        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<OralCalculateExerciseListBean>() {

                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(OralCalculateExerciseListBean exerciseListBean) {
                        if(exerciseListBean == null){
                            showToast("服务器返回数据为空");
                            return;
                        }
                        progress_lock = exerciseListBean.progress_lock;

                        oCsubjectBean = exerciseListBean.oCsubjectBean;
                        setData(exerciseListBean);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    private void setData(OralCalculateExerciseListBean exerciseListBean){
        if(exerciseListBean == null){
            return;
        }
        setHeaderTextForOralCalculate(shuShanApplication.getCurrentChildBean(),exerciseListBean.total,exerciseListBean.num,exerciseListBean.time);

        if(oCsubjectBean != null){
            ImageUtils.loadWXImageView(this,oCsubjectBean.cover,ivMathBook);
            tvProgressNow.setText("当前进度："+oCsubjectBean.outline_name);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_set_progress_math://设置进度
                gotoSetProgress();
                break;
            case R.id.ll_pre_lesson_math://上一课
                setSubjectProgress(1);
                break;
            case R.id.ll_next_lesson_math://下一课
                setSubjectProgress(2);
                break;
            case R.id.btn_start_exercise://开始练习
                if(oCsubjectBean == null){
                    showToast("获得数据为空");
                    return;
                }
                if(!(shuShanApplication.isSectVip(Constants.TYPE_ORAL_CALCULATE_EXERCISE)
                        || oCsubjectBean.state == 0)){
                    DialogUtil.showNoVipDialog(this,Constants.TYPE_ORAL_CALCULATE_EXERCISE);
                    return;
                }
                Intent intent = new Intent(OralCalculateExerciseActivity.this,OralCalculateExamActivity.class);
                startActivityAllow(intent);
                break;
        }
    }

    /**
     * 设置下一课和上一课
     * @param type 1:上一课；2下一课
     */
    private void setSubjectProgress(int type) {
        if(progress_lock == 0){
            showToast("学生不可以调节进度");
            return;
        }

        int childId = shuShanApplication.getCurrentChildId();

        int outline_id = 0;
        int outline_detail_id = 0;
        if(oCsubjectBean != null){
            outline_id = oCsubjectBean.id;
            outline_detail_id = oCsubjectBean.outline_detail_id;
        }

        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.OC_PRE_NEXT_TOPIC_SET);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id", childId)
                .putValue("type", type + "")
                .putValue("outline_id", outline_id + "")
                .putValue("outline_detail_id", outline_detail_id + "");
        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(data);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        if(jsonObject != null){
                            int outlineDetailId = jsonObject.optInt("outline_detail_id",0);
                            String outlineName = jsonObject.optString("outline_name","");
                            setSwitchProgress(outlineDetailId,outlineName);
                        }

                        if (!shuShanApplication.isSectVip(Constants.TYPE_ORAL_CALCULATE_EXERCISE)){
                            getData();
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {

                        showNetError(netRetBean);
                    }
                });
    }


    private void setSwitchProgress(int outlineDetailId, String outlineName){
        if(TextUtils.isEmpty(outlineName) || outlineDetailId <= 0){
            return;
        }

        tvProgressNow.setText("当前进度：" + outlineName);
        oCsubjectBean.outline_detail_id = outlineDetailId;
        oCsubjectBean.outline_name = outlineName;
    }

    private void gotoSetProgress() {
        if(progress_lock == 0){
            showToast("学生不可以调节进度");
            return;
        }

        if(oCsubjectBean != null){
            Intent intent = new Intent(this,StudyProgressSetActivity.class);
            intent.putExtra("subjectId",oCsubjectBean.subject_id);
            intent.putExtra("outlineId",oCsubjectBean.id);
            intent.putExtra("outlineDetailId",oCsubjectBean.outline_detail_id);
            intent.putExtra("practiceType",Constants.TYPE_ORAL_CALCULATE_EXERCISE);
            startActivityForResultAllow(intent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == 100){
            getData();
        }
    }

}
